namespace Microsoft.Robotics.Tracing
{
    using System;
    using System.Diagnostics;
    using System.Globalization;
    using System.Reflection;

    /// <summary>
    /// An implementation of trace writer based on System.Diagnostics trace class
    /// </summary>
    public class SimpleFormatter : ITraceFormatter
    {
        /// <summary>
        /// Format trace string
        /// </summary>
        /// <param name="dateTime">Date time</param>
        /// <param name="level">Trace level</param>
        /// <param name="context">Trace context</param>
        /// <param name="format">Input string format</param>
        /// <param name="args">The arguments</param>
        /// <returns>Formatted trace string</returns>
        public string Format(DateTime dateTime, TraceContext level, TraceContext context, string format, params object[] args)
        {
            /*
            // Slowest ~220,00 formats per second, stddev = 30k
            return string.Concat(
                   dateTime.ToString("o"),
                   "\t",
                   level.Text,
                   "\t",
                   context.Text,
                   "\t",
                   string.Format(format, args));*/

            /*
            // Faster ~450,00 formats per second, stddev = 30k
            return string.Format(
                "{0}-{1}-{2}\t{3}:{4}:{5}.{6}\t{7}\t{8}\t{9}",
                dateTime.Year,
                dateTime.Month,
                dateTime.Day,
                dateTime.Hour,
                dateTime.Minute,
                dateTime.Second,
                dateTime.Millisecond,
                level.Text,
                context.Text,
                string.Format(format, args)); */

            /*
                // Even faster ~490,00 per sec, stddev = 30k
                return string.Concat( 
                        dateTime.Year.ToString(),
                        "-",
                        dateTime.Month.ToString(),
                        "-",
                        dateTime.Day.ToString(),
                        " ",
                        dateTime.Hour.ToString(),
                        ":",
                        dateTime.Minute.ToString(),
                        ":",
                        dateTime.Second.ToString(),
                        ".",
                        dateTime.Millisecond.ToString(),
                        "\t",                    
                        level.Text,
                        "\t",
                        context.Text,
                        "\t",
                        string.Format(format, args)); */
           
           // Fastest avg of ~550,000 per sec, stddev = 30k
           return string.Concat( 
                   dateTime.ToString("yy-MM-d H:mm:ss.fff\t", CultureInfo.InvariantCulture),
                   level.Text,
                   "\t",
                   context.Text,
                   "\t",
                   string.Format(format, args));
        }
    }
}
